#include "widgetcalque.h"
#include "ui_widgetcalque.h"
#include "mainwindow.h"
#include "fenetreimage.h"
#include "imagecomposer.h"
#include <iterator>
#include <QtGui>
using namespace std;

WidgetCalque::WidgetCalque(QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::WidgetCalque)
{
    mw = (MainWindow*) parent;
    ui->setupUi(this);


    QObject::connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(toggleItem(QListWidgetItem*)));

}

WidgetCalque::~WidgetCalque()
{
    delete ui;
}


void WidgetCalque::reactu() {
    if(mw->getMdiArea()->activeSubWindow()) {
        FenetreImage* fi = (FenetreImage*) mw->getMdiArea()->activeSubWindow()->widget();
        vector<Calque*> vcal = fi->vectCalques;
        vector<Calque*>::reverse_iterator It;
        ui->listWidget->clear();
        for(It= vcal.rbegin(); It != vcal.rend(); ++It){
            QListWidgetItem* itm = new QListWidgetItem(*(*It)->getNom());
            itm->setFlags(itm->flags() |Qt::ItemIsUserCheckable);
            if((*It)->isVisible()) {
                itm->setCheckState(Qt::Checked);
            } else {
                itm->setCheckState(Qt::Unchecked);
            }
            ui->listWidget->addItem(itm);
        }
    }
}

void WidgetCalque::paintEvent(QPaintEvent *)
{
    if(mw->getMdiArea()->activeSubWindow() == 0) {
        this->setEnabled(false);
    } else {
        this->setEnabled(true);
    }
}

void WidgetCalque::toggleItem(QListWidgetItem* item) {
    int count = ui->listWidget->count();
    int index;
    for(index = 0; index < count; index++) {
        if(item == ui->listWidget->item(index))
            break;
    }
    if(item != ui->listWidget->item(index)) {
        cerr << "ALLERT" << endl;
    }
    index = (count-1)-index;
    FenetreImage* fi = (FenetreImage*) mw->getMdiArea()->activeSubWindow()->widget();
    vector<Calque*> vcal = fi->vectCalques;
    fi->setIndexCourant(index);
    if(item->checkState() == Qt::Checked) {
        fi->setCalqueVisible(true, index);
    } else {
        fi->setCalqueVisible(false, index);
    }
}

void WidgetCalque::on_nouveau_clicked()
{
    if(mw->getMdiArea()->activeSubWindow()->widget()) {
        FenetreImage* fi = (FenetreImage*) mw->getMdiArea()->activeSubWindow()->widget();
        fi->nouveauCalque();
        reactu();
    }
}

void WidgetCalque::on_supprimer_clicked()
{
    if(mw->getMdiArea()->activeSubWindow()->widget()) {
        FenetreImage* fi = (FenetreImage*) mw->getMdiArea()->activeSubWindow()->widget();
        fi->supprimerCalqueCourant();
        reactu();
    }
}

void WidgetCalque::on_spinBox_valueChanged(int opacite)
{
    if(mw->getMdiArea()->activeSubWindow()->widget()) {
        FenetreImage* fi = (FenetreImage*) mw->getMdiArea()->activeSubWindow()->widget();
        fi->setOpaciteCalqueCourant(opacite);

    }
}

void WidgetCalque::on_pushButton_clicked()
{
    if(mw->getMdiArea()->activeSubWindow()->widget()) {
        FenetreImage* fi = (FenetreImage*) mw->getMdiArea()->activeSubWindow()->widget();

        Calque* copie = fi->getCalqueCourant()->copie();
        ElementHistorique* eh = new ElementHistorique(QString("fusion"), copie);
        fi->historique.ajouter(eh);

        Calque* calque = fi->getCalqueCourant();
        QImage nouveau = fi->getCalqueCourant()->copy();
        ImageComposer* composer = new ImageComposer(&nouveau, calque,this);
        int res = composer->exec();

        if(res == QDialog::QDialog::Accepted) {
            QPainter p(fi->getCalqueCourant());
            p.drawImage(0,0,nouveau);
            p.end();


            fi->updateCalqueCourant();
        }

        delete composer;
    }
}

void WidgetCalque::on_pushButton_2_clicked()
{
    FenetreImage* fi = (FenetreImage*) mw->getMdiArea()->activeSubWindow()->widget();
    //selection de l'image
    QString* selectedImage = new QString;
    *selectedImage = QFileDialog::getOpenFileName(this, "Ouvrir une image", QString(), "Images (*.png *pnm *.gif *.jpg *.jpeg *.bmp)");
    //affichage de l'image

    if(!selectedImage->isEmpty()){
        //si l utilisateur a selectionne une image on la charge
        Calque* image = new Calque;
        image->load(*selectedImage);
        image->updateInfo();
        fi->ajouterCalque(image);
    } else {
        //std::cout << "Pas d'image selectionne" << std::endl;
    }

    delete selectedImage;
}
